const FileUpload = require('../../tools/FileUpload');
module.exports = class extends think.Controller
{
    /**
     * 上传一个文件公用接口
     * @returns {*}
     */
    uploddFileAction()
    {
        let upFileObj = this.ctx.file();
        let fileArr = [];
        for (let item in upFileObj)
        {
            fileArr.push(upFileObj[item]);
        }
        if (fileArr.length <= 0)
        {
            this.ctx.fail(50002, '没有选择上传文件');
            return false;
        }
        if (fileArr.length > 1)
        {
            this.ctx.fail(50001, '只能上传一个文件');
            return false;
        }

        let fileObj = new FileUpload(fileArr[0], think.ROOT_PATH + '/www/uploads/imgs/');
        let fileInfo = fileObj.save();

        if (think.isEmpty(fileInfo))
        {
            this.ctx.fail(51001, '移动上传文件失败');
            return false;
        } else
        {
            this.ctx.fail(0, '文件上传成功', fileInfo);
            return false;
        }
    }


    richEditorUploadFileAction()
    {
        let upFileObj = this.file('uploadimg');
        if (upFileObj == undefined)
        {
            return this.ctx.body = '没有上传文件(name必须为uploadimg)';
        }

        let fileObj = new FileUpload(upFileObj, think.ROOT_PATH + '/www/uploads/imgs/');
        let fileInfo = fileObj.save();
        let saveFileName = fileInfo.saveFile;

        //获取主机
        let origin = this.ctx.origin;

        this.ctx.body = origin + saveFileName;
        return false;
    }
};